#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <queue>
#include <string>


using namespace std;

class WordFreq
{
	struct data
	{
		string ord;
		int n;

		data()
		{
			ord = "";
			n = 0;
		}


		friend bool operator< (const data &lop, const data &rop)
		{ return lop.n < rop.n; }



	};

private:
	map <string, int> Mupp;
	priority_queue<data> pq;

public:



	WordFreq(void)
	{
	}

	void ReadFromFile()
	{
		ifstream filein;
		string s;
		filein.open("nils_holgersson.txt");
		while (filein.good())
		{
			filein >> s;
			Mupp[s]++;
		}
	}

	void No1Word()
	{
		int Maximus = 0;
		string Word;
		for(auto &i : Mupp)
		{
			if (i.second > Maximus)
			{
				Maximus = i.second;
				Word = i.first;
			}
		}
		cout << "Most common word is: [" << Word << "] it was found: " << Maximus << " times." << endl;
	}
	void Top20Words()
	{
		data oui;
		for(auto &i : Mupp)
		{
			oui.ord = i.first;
			oui.n = i.second;
			pq.push(oui);
		}
		string word;
		for(int i = 0 ; i < 20; i++)
		{
			cout << "Word [" << pq.top().ord << "] was found: " << pq.top().n << " times." << endl;
			pq.pop();

		}
	}
	~WordFreq(void)
	{
	}
};